<template>
  <div class="newOrder">
    <el-breadcrumb separator="/" class="breadcrumb-bottom">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>教材管理</el-breadcrumb-item>
      <el-breadcrumb-item>新增订单</el-breadcrumb-item>
    </el-breadcrumb>
    <el-row :gutter="20" class="mb-12">
      <el-col :span="5">
        <el-select
          class="full-line"
          v-model="form.majorId"
          filterable
          clearable
          placeholder="请选择专业名称">
          <el-option
            v-for="item in majorChoose"
            :label="item.majorName"
            :value="item.majorID">
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="5">
        <el-date-picker
          @change="getGrade"
          v-model="form.grade"
          type="year"
          placeholder="请选择年级"
          class="full-line">
        </el-date-picker>
      </el-col>
      <el-col :span="5">
        <el-date-picker
          @change="getAcademicYear"
          v-model="form.academicYear"
          type="year"
          placeholder="请选择学年"
          class="full-line">
        </el-date-picker>
      </el-col>
      <el-col :span="5">
        <el-select
          class="full-line"
          v-model="form.term"
          placeholder="请选择学期">
          <el-option label="01" value="01"></el-option>
          <el-option label="02" value="02"></el-option>
        </el-select>
      </el-col>
      <el-col :span="4" class="overflow-clear">
        <el-button type="primary" @click="searchOrder" class="float-right">立即搜索</el-button>
      </el-col>
    </el-row>
    <hr>
    <!--
    <div class="last-order">
      <div class="overflow-clear">
        <div class="float-right show-history">
          <span>点击<b>显示/隐藏</b>上一个历史订单</span>
          <el-switch
            v-model="tableSwitch"
            on-text=""
            off-text="">
          </el-switch>
        </div>
      </div>
      <div v-show="tableSwitch">
        <el-table
          :data="lastOrderTable"
          style="width : 100%">
          <el-table-column
            prop="publishingHouse"
            label="出版社"
            min-width="130">
          </el-table-column>
          <el-table-column
            prop="textbookName"
            label="教材名称"
            min-width="130">
          </el-table-column>
          <el-table-column
            prop="author"
            label="作者"
            min-width="100">
          </el-table-column>
          <el-table-column
            prop="price"
            label="单价"
            min-width="100">
          </el-table-column>
          <el-table-column
            prop="isbn"
            label="ISBN"
            min-width="130">
          </el-table-column>
          <el-table-column
            prop="courseNameCN"
            label="课程名称"
            min-width="130">
          </el-table-column>
          <el-table-column
            prop="majorName"
            label="使用专业"
            min-width="130">
          </el-table-column>
          <el-table-column
            prop="orderNum"
            label="预定数量"
            min-width="100">
          </el-table-column>
          <el-table-column
            prop="remark1"
            label="备注"
            min-width="120">
          </el-table-column>
        </el-table>
        <div class="paging">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="pageSizes"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
        </div>
      </div>
    </div>
-->
    <div class="new-order">
      <el-row :gutter="20" class="mtb-12">
        <el-col :span="5">
          <el-date-picker
            @change="getOrderGrade"
            v-model="newOrder.grade"
            type="year"
            placeholder="请选择年级"
            class="full-line">
          </el-date-picker>
        </el-col>
        <el-col :span="5">
          <el-date-picker
            @change="getOrderAcademicYear"
            v-model="newOrder.academicYear"
            type="year"
            placeholder="请选择学年"
            class="full-line">
          </el-date-picker>
        </el-col>
        <el-col :span="5">
          <el-select
            clearable
            class="full-line"
            v-model="newOrder.term"
            placeholder="请选择学期">
            <el-option label="01" value="01"></el-option>
            <el-option label="02" value="02"></el-option>
          </el-select>
        </el-col>
        <el-col :span="9" class="clearfix">
          <el-button-group class="float-right">
            <el-button type="primary" @click="addOrder">新增订单</el-button>
            <!-- <el-button type="primary">打印</el-button>-->
          </el-button-group>
        </el-col>
      </el-row>
      <!-- dialogVisible to new order -->
      <el-dialog title="新增订单" v-model="dialogFormVisible">
        <el-form :model="addOrderForm">
          <el-form-item label="课程名称" :label-width="formLabelWidth">
            <el-select
              class="full-line"
              v-model="addOrderForm.courseId"
              filterable
              clearable
              placeholder="请选择课程名称"
              @change="getCourseTextbook">
              <el-option
                v-for="item in courseChoose"
                :label="item.courseNameCN"
                :value="item.courseId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="教材名称" :label-width="formLabelWidth">
            <el-select
              class="full-line"
              v-model="addOrderForm.textbookId"
              filterable
              clearable
              placeholder="请选择教材名称">
              <el-option
                v-for="item in textbookChoose"
                :label="item.textbookName"
                :value="item.textbookId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="使用专业" :label-width="formLabelWidth">
            <el-select
              class="full-line"
              v-model="addOrderForm.majorId"
              filterable
              clearable
              placeholder="请选择专业名称">
              <el-option
                v-for="item in majorChoose"
                :label="item.majorName"
                :value="item.majorID">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="使用班级" :label-width="formLabelWidth">
            <el-select
              class="full-line"
              v-model="addOrderForm.classId"
              filterable
              clearable
              placeholder="请选择班级名称">
              <el-option
                v-for="item in classChoose"
                :label="item.className"
                :value="item.classId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="预定数量" :label-width="formLabelWidth">
            <el-input v-model="addOrderForm.orderNum"></el-input>
          </el-form-item>
          <el-form-item label="备注" :label-width="formLabelWidth">
            <el-input type="textarea" :rows="5" v-model="addOrderForm.remark1"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button v-show="!updateState" type="primary" @click="submitDialogForm">确 定</el-button>
          <el-button v-show="updateState" type="primary" @click="UpdateDialogForm">修改</el-button>
        </div>
      </el-dialog>

      <!-- order table in form -->
      <el-table
        :data="newOrderTable"
        style="width : 100%">
        <el-table-column
          prop="publishingHouse"
          label="出版社"
          min-width="130">
        </el-table-column>
        <el-table-column
          prop="textbookName"
          label="教材名称"
          min-width="130">
        </el-table-column>
        <el-table-column
          prop="author"
          label="作者"
          min-width="100">
        </el-table-column>
        <el-table-column
          prop="price"
          label="单价"
          min-width="100">
        </el-table-column>
        <el-table-column
          prop="isbn"
          label="ISBN"
          min-width="140">
        </el-table-column>
        <el-table-column
          prop="courseNameCN"
          label="课程名称"
          min-width="150">
        </el-table-column>
        <el-table-column
          prop="majorName"
          label="使用专业"
          min-width="130">
        </el-table-column>
        <el-table-column
          prop="orderNum"
          label="预定数量"
          min-width="100">
        </el-table-column>
        <el-table-column
          prop="remark1"
          label="备注"
          min-width="120">
        </el-table-column>
        <el-table-column
          label="操作"
          min-width="120">
          <template scope="scope">
            <el-button icon="edit" type="text"
                       @click="updateNewOederItem(scope.$index,newOrderTable,scope.row.courseId)"></el-button>
            <el-button icon="delete" type="text"
                       @click="deleteNewOederItem(scope.$index,newOrderTable)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <div style="margin-top:8px;" class="overflow-clear">
        <el-button type="primary" class="float-right" @click="submitTable">确认</el-button>
        <span class="float-right" style="line-height:36px;margin:0 4px;">点击按钮确认选取订单</span>
      </div>
    </div>
  </div>
</template>
<style>
  .show-history {
    height: 46px;
    line-height: 46px;
  }

  .new-order, .last-order {
    margin-top: 4px;
    margin-bottom: 4px;
  }
</style>
<script>
  export default{
    name: 'newOrder',
    data(){
      return {
        form: {
          majorId: '',
          grade: '',
          academicYear: '',
          term: '',
          createTime: '',
          isConfirm: '2'
        },
        addOrderForm: {
          textbookId: '',
          publishingHouse: '',
          textbookName: '',
          author: '',
          price: '',
          isbn: '',

          courseId: '',
          courseNameCN: '',

          majorId: '',
          majorName: '',

          classId: '',
          orderNum: '',
          remark1: ''
        },
        newOrder: {
          grade: '',
          academicYear: '',
          term: '',
          order: []
        },
        courseForm: {
          courseId: ''
        },
        dialogFormVisible: false,
        formLabelWidth: '120px',
        //
        majorChoose: [],
        classChoose: [],
        courseChoose: [],
        textbookChoose: [],
        //page
        currentPage: 1,
        pageSizes: [10, 20, 30, 40],
        pageSize: 10,
        total: 40,
        //
        tableSwitch: false,
        newOrderTable: [],
        lastOrderTable: [],
        newOrderIndex: '',
        updateState: false,
        msg: {}
      }
    },
    components: {},
    mounted: function () {
      this.$nextTick(function () {
        this.init();
      })
    },
    methods: {
      init(){
        this.setDate();
        this.getMajorInfo();
        this.getClassInfo();
        this.getCourseInfo();
        this.getTextbookInfo();
        this.getOrder();
      },
      getOrder(){
        this.$http.post(this.$until.url + '/textBookOrder/getTextBookOrder', this.form,
          {
            params: {
              page: this.currentPage,
              pageSize: this.pageSize
            },
            emulateJSON: true
          }).then(response => {
          this.total = response.body.countNum;
          //this.lastOrderTable = response.body.orderList
          this.newOrderTable = response.body.orderList
        }, response => {
          console.log('文件获取失败');
        });
      },
      searchOrder(){
        this.form.createTime = '';
        this.getOrder();
      },
      setDate(){
        var date = new Date();
        this.form.createTime = (date.getFullYear() - 1).toString();
      },
      getMajorInfo: function () {
        this.$http.get(this.$until.url + '/baseInfoCommon/initData').then(response => {
          this.majorChoose = response.body.majorAndId
        }, response => {
          console.log('文件获取失败');
        });
      },
      getClassInfo: function () {
        this.$http.get(this.$until.url + '/baseInfoCommon/getClassNameAndId').then(response => {
          this.classChoose = response.body
        }, response => {
          console.log('文件获取失败');
        });
      },
      getCourseInfo: function () {
        this.$http.get(this.$until.url + '/textBookOrder/getCourseNameAndId').then(response => {
          this.courseChoose = response.body
        }, response => {
          console.log('文件获取失败');
        });
      },
      getTextbookInfo: function () {
        this.$http.post(this.$until.url + '/textBookManage/getTextBook', this.courseForm, {emulateJSON: true}).then(response => {
          this.textbookChoose = response.body.textBookList
        }, response => {
          console.log('文件获取失败');
        });
      },
      getGrade(val){
        this.form.grade = val
      },
      getAcademicYear(val){
        this.form.academicYear = val
      },
      getOrderGrade(val){
        this.newOrder.grade = val
      },
      getOrderAcademicYear(val){
        this.newOrder.academicYear = val
      },
      addOrder(){
        this.dialogFormVisible = true
        this.updateState = false
        for (var key in this.addOrderForm) {
          this.addOrderForm[key] = ''
        }
        this.courseForm.courseId = ''
        this.getTextbookInfo();
      },
      getNewOrderInfo(){
        var msg = {};
        for (const value of  this.majorChoose) {
          if (this.addOrderForm.majorId == value.majorID) {
            this.addOrderForm.majorName = value.majorName
          }
        }
        for (const value of  this.courseChoose) {
          if (this.addOrderForm.courseId == value.courseId) {
            this.addOrderForm.courseNameCN = value.courseNameCN
          }
        }
        for (const value of  this.textbookChoose) {
          if (this.addOrderForm.textbookId == value.textbookId) {
            this.addOrderForm.publishingHouse = value.publishingHouse
            this.addOrderForm.textbookName = value.textbookName
            this.addOrderForm.author = value.price
            this.addOrderForm.price = value.price
            this.addOrderForm.isbn = value.isbn
          }
        }
        for (var key in this.addOrderForm) {
          msg[key] = this.addOrderForm[key]
        }
        this.msg = msg
      },
      submitDialogForm() {
        this.getNewOrderInfo();
        this.newOrderTable.push(this.msg)
        this.dialogFormVisible = false
      },
      handleSizeChange(val) {
        //console.log(`每页 ${val} 条`);
        this.pageSize = val;
        this.getOrder()
      },
      handleCurrentChange(val) {
        this.currentPage = val;
        this.getOrder()
      },
      submitTable: function () {
        this.newOrder.order = this.newOrderTable
        this.$http.post(this.$until.url + '/textBookOrder/addTextBookOrder',
          this.newOrder).then((res) => {
          this.$message({
            message: res.body.message,
            type: 'success'
          });
        }, (err) => {
          this.$message.error('错了哦，这是一条错误消息');
        });
      },
      updateNewOederItem(index, rows, courseId){
        this.courseForm.courseId = courseId
        this.updateState = true
        var msg = {}
        for (var key in rows[index]) {
          msg[key] = rows[index][key]
        }
        this.addOrderForm = msg
        this.dialogFormVisible = true
        this.newOrderIndex = index
        this.getTextbookInfo();
      },
      UpdateDialogForm(){
        this.getNewOrderInfo();
        this.newOrderTable.splice(this.newOrderIndex, 1, this.msg)
        this.dialogFormVisible = false
      },
      deleteNewOederItem(index, rows){
        this.$confirm('此操作将永久删除该订单, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          rows.splice(index, 1);
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      getCourseTextbook(){
        this.courseForm.courseId = this.addOrderForm.courseId
        this.getTextbookInfo();
      }
    }
  }
</script>
